<template>
  <div>
    <div v-if="isGuest">
      <divider>请登陆</divider>
      <group label-width="4.5em" label-margin-right="2em" label-align="right">
        <x-input   type="text" placeholder="请输入您的账号" v-model="form.loginName"></x-input>
        <x-input   type="password" placeholder="请输入您的密码" v-model="form.password"></x-input>
      </group>
      <x-button type="primary" action-type="button" @click.native="login()">登陆</x-button>
    </div>
    <div class="no-auth" v-if="!isGuest">
      <icon type="warn" is-msg></icon>
      <span class="mr10 mb20">无权限访问</span>
      <x-button type="warn" action-type="button" :mini="true" @click.native="close()">关闭</x-button>
    </div>
    <!--<loading :show="isWeixn" text="登陆中..."></loading>-->
  </div>
</template>
<script>
  import { Group, XInput, XButton, Divider,Loading } from "vux";
  import { debug } from 'util';
  import Icon from "vux/src/components/icon/index";

  export default {
    components: {
      Icon,
      Group,
      XInput,
      XButton,
      Divider,
      Loading
    },
    data(){
      return {
        form:{
          loginName:null,
          password:null
        },
        isWeixn: false,
        isGuest: true,
        isWechatLogin: false,
        userInfo: {},
      }
    },
    created() {
      this.$getUserInfo().then(user=> {
        let flag = true
        user.roles.forEach(item=>{
          if (item.code == 'PDY'){
            flag = false
            this.$router.push({path: '/management/manOrderHome'})
          }
          return false
        })
        if (flag) {
          user.roles.forEach(item => {
            if (item.code == 'GCS') {
              this.$router.push({path: '/engineer/enMyOrders'})
            }
            return false
          })
        }
      })
    },
    mounted() {
      if (this.$store.state.user.userInfo.id === 'guest') {  //访客
        this.isGuest = true
        //如果是在微信浏览器中，就跳转
  /*      if (this.is_weixn()) {
          if (this.$route.query.redirect) {
            //登陆后回到当前页
            alert('登陆成功')
          } else {
            location.href = "/wechat/auth/login";
          }
        }     */
      } else{
        this.isGuest = false
      }
    },
    methods: {
      is_weixn() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
          return true;
        } else {
          return false;
        }
      },
      login(){
        this.$http.post("/wechat/auth/login",this.form).then(res=> {
          //登陆成功
          this.$vux.toast.show({
            text: '登陆成功'
          })
          setTimeout(function () {
            WeixinJSBridge.call('closeWindow')
          },1000)
        })
      },
      close() {
          WeixinJSBridge.call('closeWindow')
      }
    },

  };
</script>

<style lang="less">
  body{
    padding-top: 0.01rem;
  }
  .no-auth{
    margin-top: 2rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
</style>
